home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / prinintr.c < prev    next >
C/C++ Source or Header  |  1985-10-30  |  16KB  |  454 lines

  1. /* printer interface example v28 */
  2.  
  3. /****************************************************************
  4. *                                                               *
  5. * Copyright 1985, Commodore Amiga Inc.  All rights reserved.    *
  6. * No part of this program may be reproduced, transmitted,       *
  7. * transcribed, stored in retrieval system, or translated into   *
  8. * any language or computer language, in any form or by any      *
  9. * means, electronic, mechanical, magnetic, optical, chemical,   *
  10. * manual or otherwise, without the prior written permission of  *
  11. * Commodore Amiga Incorporated, 983 University Ave, #D          *
  12. * Los Gatos, CA 95030                                           *
  13. *                                                               *
  14. ****************************************************************/
  15. /*** cdio.c ***/
  16.  
  17. /*********************************************************************/
  18. /*                                                                   */
  19. /*                     Copyright (c) 1985                            */
  20. /*                    Commodore-Amiga, Inc.                          */
  21. /*                    All rights reserved.                           */
  22. /*                                                                   */
  23. /*     No part of this program may be reproduced, transmitted,       */
  24. /*     transcribed, stored in retrieval system, or translated        */
  25. /*     into any language or computer language, in any form or        */
  26. /*     by any means, electronic, mechanical, magnetic, optical,      */
  27. /*     chemical, manual or otherwise, without the prior written      */
  28. /*     permission of:                                                */
  29. /*                     Commodore-Amiga, Inc.                         */
  30. /*                     983 University Ave #D                         */
  31. /*                     Los Gatos, CA. 95030                          */
  32. /*                                                                   */
  33. /*********************************************************************/
  34.  
  35. /*********************************************************************/
  36. /*                                                                   */
  37. /*  Program name:  cdio                                              */
  38. /*                                                                   */
  39. /*  Purpose:  To provide standard console device interface routines  */
  40. /*            such as Open, GetChar, PutChar, etc.                   */
  41. /*                                                                   */
  42. /*  Arguments:  (window)    - The window to associate the console    */
  43. /*                            device with. Used in CDOpen.           */
  44. /*              (character) - The char to output. Used in CDPutChar  */
  45. /*              (string)    - The string to output. Used in CDPutStr */
  46. /*                                                                   */
  47. /*  Programer:  Stan Shepard                                         */
  48. /*                                                                   */
  49. /*  Date Released:  01-Jun-85                                        */
  50. /*                                                                   */
  51. /*********************************************************************/
  52.  
  53. /*#include "intuall.h"*/
  54. #include "idemoall.h"
  55.  
  56. struct IOStdReq consoleIO = 0;
  57. struct IOStdReq consoleREADIO = 0;
  58. struct MsgPort consoleMsgPort = 0;
  59. UBYTE consoleReadChar = 0;
  60.  
  61. int CDOpen(window)
  62. struct Window *window;
  63. {
  64.         int error;
  65.                 
  66. /* Open the console device */
  67.         consoleIO.io_Data = (char *) window;
  68.         consoleIO.io_Length = sizeof(*window);
  69.         if ((error = OpenDevice("console.device", 0, &consoleIO, 0)) != 0)
  70.         {
  71.                 kprintf("CDInit OpenDevice error: %d.\n", error);
  72.                 return(error);
  73.         }
  74.  
  75. /* Set up the message port in the I/O request */
  76.         consoleMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  77.         consoleMsgPort.mp_Flags = 0;
  78.         consoleMsgPort.mp_SigBit = AllocSignal(-1);
  79.         consoleMsgPort.mp_SigTask = (struct Task *) FindTask((char *) NULL);
  80.         AddPort(&consoleMsgPort);
  81.         consoleIO.io_Message.mn_ReplyPort = &consoleMsgPort;
  82.  
  83. /* Start reading */
  84.         consoleREADIO = consoleIO;
  85.         queue_read();
  86.  
  87.         return(0);
  88. }
  89.  
  90. CDClose()
  91. {
  92.         RemPort(&consoleMsgPort);
  93.         CloseDevice(&consoleIO);
  94. }
  95.  
  96. int CDMayGetChar()
  97. {
  98.         register temp;
  99.  
  100.         if ( GetMsg(&consoleMsgPort) == NULL ) return(-1);
  101.         temp = consoleReadChar;
  102.         queue_read();
  103.         return(temp);
  104. }
  105.  
  106. queue_read()
  107. {
  108.         consoleREADIO.io_Command = CMD_READ;
  109.         consoleREADIO.io_Data = &consoleReadChar;
  110.         consoleREADIO.io_Length = 1;
  111.         SendIO(&consoleREADIO);                   /* Due to bug in DoIO */
  112. }
  113.  
  114. UBYTE CDGetChar()
  115. {
  116.         register temp;
  117.  
  118.         while (GetMsg(&consoleMsgPort) == NULL) ; /* during console IO */
  119.         temp = consoleReadChar;
  120.         queue_read();
  121.         return(temp);
  122. }
  123.  
  124. CDPutChar(character)
  125. char character;
  126. {
  127.         consoleIO.io_Command = CMD_WRITE;
  128.         consoleIO.io_Data = &character;
  129.         consoleIO.io_Length = 1;
  130.         DoIO(&consoleIO);
  131. }
  132.  
  133. CDPutStr(string)
  134. char *string;           /*  NULL termiinated string to output  */
  135. {
  136.         consoleIO.io_Command = CMD_WRITE;
  137.         consoleIO.io_Data = string;
  138.         consoleIO.io_Length = 0;
  139.         DoIO(&consoleIO);
  140. }
  141. /*** pio.c ***/
  142. #include        "exec/types.h"
  143. #include        "exec/nodes.h"
  144. #include        "exec/lists.h"
  145. #include        "exec/memory.h"
  146. #include        "exec/ports.h"
  147. #include        "exec/libraries.h"
  148. #include        "exec/devices.h"
  149. #include        "exec/tasks.h"
  150. #include        "exec/io.h"
  151. #include        "exec/interrupts.h"
  152. #include        "../printer/printer.h"
  153.  
  154. union {
  155.     struct IOStdReq ios;
  156.     struct IODRPReq iodrp;
  157.     struct IOPrtCmdReq iopc;
  158. } printerIO;
  159.  
  160. struct MsgPort replyMsgPort;
  161.  
  162. VOID pWrite(buffer,count)
  163. char *buffer;
  164. long count;
  165. {
  166.     char *b;
  167.     /* queue a printer write */
  168.     printerIO.ios.io_Command = CMD_WRITE;
  169.     printerIO.ios.io_Data = buffer;
  170.     printerIO.ios.io_Length = count;
  171.     DoIO(&printerIO);
  172. };
  173.  
  174. VOID pDumpRPort(rastPort, colorMap, modes, sx,sy, sw,sh, dc,dr, special)
  175. struct RastPort *rastPort;
  176. UWORD *colorMap;
  177. ULONG modes;
  178. int sx, sy, sw, sh, dc, dr;
  179. UWORD special;
  180. {
  181.     printerIO.iodrp.io_Command = PRD_DUMPRPORT;
  182.     printerIO.iodrp.io_RastPort = rastPort;
  183.     printerIO.iodrp.io_ColorMap = colorMap;
  184.     printerIO.iodrp.io_Modes = modes;
  185.     printerIO.iodrp.io_SrcX = sx;
  186.     printerIO.iodrp.io_SrcY = sy;
  187.     printerIO.iodrp.io_SrcWidth = sw;
  188.     printerIO.iodrp.io_SrcHeight = sh;
  189.     printerIO.iodrp.io_DestCols = dc;
  190.     printerIO.iodrp.io_DestRows = dr;
  191.     printerIO.iodrp.io_Special = special;
  192.     DoIO(&printerIO);
  193. };
  194.  
  195. int pOpen(signal)
  196. int signal;
  197. {
  198.     int error;
  199.     kprintf("  attempting to open the printer device \n");          
  200.     /* open the printer device */
  201.     if ((error = OpenDevice("printer.device", 0, &printerIO, 0)) != 0)
  202.     {
  203.     kprintf("EventInit \"printer.device\" OpenDevice error: %d.\n", error);
  204.         return(error);
  205.     }
  206.     kprintf("ior.error= %ld\n", printerIO.ios.io_Error);
  207.     kprintf("printer.device: %lx.\n", printerIO.ios.io_Device);
  208.  
  209.     /* set up the message port in the I/O request */
  210.     replyMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  211.     replyMsgPort.mp_Flags = 0;
  212.     if (signal >= 0)
  213.     {
  214.         kprintf("Printer signal: %ld\n", signal);
  215.         replyMsgPort.mp_SigBit = signal;
  216.         replyMsgPort.mp_SigTask = (struct Task *) FindTask((char *) NULL);
  217.     }
  218.     else
  219.     {
  220.         kprintf("Warning.  printer set up for immediate I/O only.\n");
  221.         replyMsgPort.mp_SigBit = 0;
  222.         replyMsgPort.mp_SigTask = 0;
  223.     }
  224.     AddPort(&replyMsgPort);
  225.     printerIO.ios.io_Message.mn_ReplyPort = &replyMsgPort;
  226. }
  227.  
  228. pClose()
  229. {
  230.     CloseDevice(&printerIO);
  231.     RemPort(&replyMsgPort);
  232. }
  233. /*** test.c ***/
  234. /*********************************************************************/
  235. /*                                                                   */
  236. /*                     Copyright (c) 1985                            */
  237. /*                    Commodore-Amiga, Inc.                          */
  238. /*                    All rights reserved.                           */
  239. /*                                                                   */
  240. /*     No part of this program may be reproduced, transmitted,       */
  241. /*     transcribed, stored in retrieval system, or translated        */
  242. /*     into any language or computer language, in any form or        */
  243. /*     by any means, electronic, mechanical, magnetic, optical,      */
  244. /*     chemical, manual or otherwise, without the prior written      */
  245. /*     permission of:                                                */
  246. /*                     Commodore-Amiga, Inc.                         */
  247. /*                     983 University Ave #D                         */
  248. /*                     Los Gatos, CA. 95030                          */
  249. /*                                                                   */
  250. /*********************************************************************/
  251.  
  252. /*********************************************************************/
  253. /*                                                                   */
  254. /*  Program name:  printer test (built on pic)                       */
  255. /*                                                                   */
  256. /*  Arguments:  (none)                                               */
  257. /*                                                                   */
  258. /*  Notes:  1.  Open various libraries.                              */
  259. /*          2.  Open a custom screen and window.                     */
  260. /*          3.  Open & associate a console device with the window.   */
  261. /*          4.  Draw some simple colored patterns into the window.   */
  262. /*          5.  Execute printer commands from the console.           */
  263. /*          6.  Close the window.                                    */
  264. /*                                                                   */
  265. /*  Programer:  Stan Shepard                                         */
  266. /*                                                                   */
  267. /*  Date Released:  11-Jun-85                                        */
  268. /*                                                                   */
  269. /*  Modified:   14-Jun-85 by David Berezowski for V25.22 update      */
  270. /*              15-Jun-85 by Kodiak for printer device test          */
  271. /*              17-Jun-85 by Andy for parallel&printer test
  272. /*                                                                   */
  273. /*********************************************************************/
  274.  
  275. #include        "intuall.h"
  276. #include        "../printer/printer.h"
  277.  
  278. #define getc() Read(stdin, c, 2)
  279. #define TXHEIGHT 8
  280.  
  281. static UWORD colormap[] = {     0xfff, 0xf00, 0x0f0, 0x00f,
  282.                                 0xff0, 0xf0f, 0x0ff, 0x000};
  283.  
  284. struct TextAttr TestFont =
  285.     {
  286.     "topaz.font",
  287.     TXHEIGHT,
  288.     0,
  289.     0,
  290.     };
  291.  
  292. long GfxBase = 0;
  293. long IntuitionBase = 0;
  294.  
  295. struct Screen *OpenScreen();
  296. struct Window *OpenWindow();
  297.  
  298.  
  299. main()
  300. {
  301.         struct Window *w;
  302.         struct Screen *screen;
  303.         struct RastPort *rp, *cdrp;
  304.         struct ViewPort *vp;
  305.         struct IntuiMessage *message;
  306.         ULONG cdio;
  307.         SHORT i;
  308.         SHORT inputChar;
  309.         int ch;
  310.         UBYTE c[10];
  311.         char *b, buffer[80];
  312.         int Length;
  313.         static char *slash ="/";
  314.         GfxBase = OpenLibrary("graphics.library", 0);
  315.         if (GfxBase == NULL)
  316.         {
  317.                 printf("Unable to open graphics library\n");
  318.                 exit();
  319.         }
  320.  
  321.         IntuitionBase = OpenLibrary("intuition.library", 0);
  322.         if (IntuitionBase == NULL)
  323.         {
  324.                 printf("Unable to open Intuition library\n");
  325.                 exit();
  326.         }
  327.  
  328. /*  Open a custom HIRES screen 3 planes deep  */
  329.         {
  330.                 struct NewScreen ns;
  331.                 ns.LeftEdge = 0;
  332.                 ns.TopEdge = 0;
  333.                 ns.Width = 640;
  334.                 ns.Height = 200;
  335.                 ns.Depth = 3;
  336.                 ns.DetailPen = 0;
  337.                 ns.BlockPen = 1;
  338.                 ns.ViewModes = HIRES;
  339.                 ns.Type = CUSTOMSCREEN;
  340.                 ns.Font = &TestFont;
  341.                 ns.DefaultTitle = "HIGH RES, 3 planes, for printer test";
  342.                 ns.Gadgets = NULL;
  343.                 screen = OpenScreen(&ns);       /* open the screen! */
  344.         }
  345.  
  346.         if (screen == NULL)
  347.         {
  348.                 printf("Can't open a new screen\n");
  349.                 exit();
  350.         }
  351.  
  352.         {
  353.                 struct NewWindow nw;
  354.                 nw.LeftEdge = 0;
  355.                 nw.TopEdge = 0;
  356.                 nw.Width = 640;
  357.                 nw.Height = 200;
  358.                 nw.DetailPen = -1;
  359.                 nw.BlockPen = -1;
  360.                 nw.Flags = WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|WINDOWCLOSE|ACTIVATE;
  361.                 nw.IDCMPFlags = CLOSEWINDOW;
  362.                 nw.FirstGadget = 0;
  363.                 nw.CheckMark = 0;
  364.                 nw.Title = "printer test window";
  365.                 nw.Type = CUSTOMSCREEN;
  366.                 nw.Screen = screen;
  367.                 nw.BitMap = NULL;
  368.                 nw.MinWidth = 80;
  369.                 nw.MinHeight = 15;
  370.                 nw.MaxWidth = 10000;
  371.                 nw.MaxHeight = 10000;
  372.                 w = OpenWindow(&nw);    /* open the window! */
  373.         }
  374.  
  375.         rp = w->RPort;
  376.         vp = &w->WScreen->ViewPort;
  377.  
  378. /*  Open the console device  */
  379.         CDOpen(w);
  380.  
  381. /*  Draw some horizontal bars  */
  382.         for (i = 1; i < 8; i++)
  383.         {
  384.                 SetAPen(rp,i);
  385.                 RectFill(rp,20,i*10+20,550,i*10+30);
  386.         }
  387.  
  388. /*  Draw some verticle bars  */
  389.         for (i = 1; i < 8; i++)
  390.         {
  391.                 SetAPen(rp,i);
  392.                 RectFill(rp,0+(i-1)*40,100,39+(i-1)*40,160);
  393.         }
  394.  
  395. /*  Set the color registers  */
  396.         SetRGB4(vp,0,15,15,15);         /*  White    */
  397.         SetRGB4(vp,1,15,00,00);         /*  Red      */  
  398.         SetRGB4(vp,2,00,15,00);         /*  Green    */
  399.         SetRGB4(vp,3,00,00,15);         /*  Blue     */  
  400.         SetRGB4(vp,4,15,15,00);         /*  Yellow   */
  401.         SetRGB4(vp,5,15,00,15);         /*  Magenta  */
  402.         SetRGB4(vp,6,00,15,15);         /*  Cyan     */  
  403.         SetRGB4(vp,7,00,00,00);         /*  Black    */
  404.  
  405.     kprintf("use console\n");
  406.         CDPutStr("\33[20h");            /* line feed new line mode */
  407.     kprintf("open printer\n");
  408.         pOpen(AllocSignal(-1));
  409.     kprintf("printer is open\n");
  410. /*  Wait for a CLOSEWINDOW */
  411.         CDPutStr("\nEnter command [w,c]: ");
  412. while (TRUE)
  413.         {
  414.         message = (struct IntuiMessage *)GetMsg(w->UserPort);
  415.         if((message->Class)==CLOSEWINDOW)
  416.            {
  417.            kprintf("closing up\n");
  418.            CDClose();
  419.            pClose();
  420.            CloseWindow(w);
  421.            CloseScreen(screen);
  422.            OpenWorkBench();
  423.            exit(0);
  424.            }
  425.  
  426.         if((ch=CDMayGetChar()) != -1)
  427.                 {
  428.                 CDPutChar(ch);
  429.                 switch (ch) {
  430.                 case 'w':
  431.                         CDPutStr("\n    string: ");
  432.                         b = buffer;
  433.                         while((*b = CDGetChar()) != '.') 
  434.                            {if (*b == '/') { *b++ ='\033'; CDPutChar(*slash);}
  435.                              else CDPutChar(*b++);
  436.                            };
  437.                         CDPutChar(*b++='\n');
  438.                         *b = '\0';
  439.                         pWrite(buffer,-1);
  440.                         break;
  441.                 case 'c':
  442.                         pWrite("Thi\033[4mtest\033[0ms is a test\nnow for\033[4",-1);
  443.                         pWrite("munderlineok ?????\n",-1);
  444.                         pWrite("ok ?????\n",-1);
  445.                         pWrite("564 characters were in this test line, have fun.",-1);
  446.                         pWrite("\nlast line\n",-1);
  447.                         break;
  448.                 default:;
  449.                 }
  450.            CDPutStr("\nEnter command [w,c]: ");
  451.            }    
  452.         }
  453. }
  454.